/**
 * Package:com.afteryuan.util.code;
 * $Id$
 * Copyright(c) 2001-2009 www.afteryuan.com
 */
package com.afteryuan.util.code;

import com.afteryuan.util.FileUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * ServiceTestGenerator
 * <p/>
 * <p><a href="ServiceTestGenerator.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@gmail.com">$Author$</a>
 * @version $Revision$
 */
public class ServiceTestGenerator extends ServiceGenerator {
    public static final String CLASS_STUFFIX_NAME = "Test";
    protected File serviceTestFile;
    protected String serviceTestPackageName;
    protected String serviceTestClassName;
    protected String serviceTestFQClassName;

    /**
     * 构造器
     *
     * @param model   领域模型文件
     * @param srcRoot 源代码的根目录
     */
    protected ServiceTestGenerator(File model, File srcRoot) {
        super(model, srcRoot);
    }

    /**
     * 构造器
     *
     * @param model    领域模型文件
     * @param srcRoot  源代码的根目录
     * @param testRoot 测试代码的根目录
     */
    protected ServiceTestGenerator(File model, File srcRoot, File testRoot) {
        this(model, srcRoot);
        serviceTestPackageName = servicePackageName;
        serviceTestClassName = serviceClassName + CLASS_STUFFIX_NAME;
        serviceTestFQClassName = serviceTestPackageName + "." + serviceTestClassName;
        serviceTestFile = getServiceTestFile(testRoot);
    }

    @Override
    public void generate() throws IOException {
        if(serviceTestFile.exists() && !isReplace)return;
        FileUtil.createFile(serviceTestFile);
        FileWriter fw = new FileWriter(serviceTestFile);
        fw.write("/**\n" +
                " * Package:" + serviceTestPackageName + ";\n" +
                " * $Id$\n" +
                " * Copyright(c) Spires Zhang.\n" +
                " */\n");
        fw.write("package " + serviceTestPackageName + ";\n\n");
        fw.write("import " + modelFQClassName + ";\n\n");
        fw.write("import " + serviceFQClassName + ";\n\n");
        fw.write("import " + modelRootPackageName + "." + SERVICE_SINGLE_PACKAGENAME + ".BaseServiceTestCase;\n");

        fw.write("import org.apache.commons.logging.Log;\n");
        fw.write("import org.apache.commons.logging.LogFactory;\n\n");
        fw.write("/**\n" +
                " * " + serviceTestClassName + "\n" +
                " * <p/>\n" +
                " * <p><a href=\"" + serviceTestClassName + ".java.html\"><i>View Source</i></a></p>\n" +
                " *\n" +
                " * //@author <a href=\"mailto:afteryuan//@afteryuan.com\">$" + "Author" + "$</a>\n" +
                " * //@version $" + "Revision" + "$\n" +
                " */\n");
        fw.write("public class " + serviceTestClassName + " extends BaseServiceTestCase {\n" +
                "\tprivate static Log log = LogFactory.getLog(" + serviceTestClassName + ".class);\n" +
                
                "}");
        fw.close();
        System.out.println("generate file:"+serviceTestFile);
    }

    private File getServiceTestFile(File testRoot) {

        return new File(testRoot, fqn2path(serviceTestFQClassName));
    }

    private String fqn2path(String serviceTestFQClassName) {
        return serviceTestFQClassName.replaceAll("[.]", "\\\\") + ".java";
    }


}
